//
//  CHRollView.m
//  CarFile
//
//  Created by 陈浩 on 2017/3/28.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHRollView.h"
#import "CHNoticeModel.h"
@interface CHRollView ()
{
    NSTimer *_timer;     //定时器
    int count;
    int flag; //标识当前是哪个view显示(currentView/hidenView)
    
}

//@property (nonatomic,strong) UIView *currentView;   //当前显示的view
//@property (nonatomic,strong) UIView *hidenView;     //底部藏起的view

//当前显示的内容
@property (nonatomic,strong) UILabel *currentLabel;


//未显示的内容
@property (nonatomic,strong) UILabel *hidenLabel;


@end

@implementation CHRollView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
    
}

-(void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    self.currentLabel.text = dataArr[count];
    
}


- (void)createUI
{
    count = 0;
    flag = 0;
    
    self.layer.masksToBounds = YES;
    //内容标题
    self.currentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.currentLabel.text = self.dataArr[count];
    self.currentLabel.numberOfLines = 0;
    self.currentLabel.textAlignment = NSTextAlignmentLeft;
    self.currentLabel.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0];
    self.currentLabel.font = Font(13);
    [self addSubview:self.currentLabel];

    //内容标题
    self.hidenLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height)];
    self. hidenLabel.numberOfLines= 0;
    self.hidenLabel.textAlignment = NSTextAlignmentLeft;
    self.hidenLabel.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0];
    self.hidenLabel.font = Font(13);
    [self addSubview:self.hidenLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
    [self addGestureRecognizer:tap];
    //改进
    UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dealLongPress:)];
    [self addGestureRecognizer:longPress];
    
}

- (void)dealTap:(UITapGestureRecognizer *)tap
{
    if (self.clickBlock) {
        self.clickBlock(count);
    }
}

-(void)dealLongPress:(UILongPressGestureRecognizer*)longPress{
    
    if(longPress.state==UIGestureRecognizerStateEnded){
        
        _timer.fireDate=[NSDate distantPast];
        
        
    }
    if(longPress.state==UIGestureRecognizerStateBegan){
        
        _timer.fireDate=[NSDate distantFuture];
    }
    
}


#pragma mark - 跑马灯操作
-(void)dealTimer
{
    count++;
    if (count == _dataArr.count) {
        count = 0;
    }
    
    if (flag == 1) {
  
      
        self.currentLabel.text = _dataArr[count];
        
       
    }
    
    if (flag == 0) {
      
        self.hidenLabel.text = _dataArr[count];
        
    }
    
    
    if (flag == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.currentLabel.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            flag = 1;
            self.currentLabel.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            self.hidenLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            
        }];
    }else{
        
        [UIView animateWithDuration:0.5 animations:^{
            self.hidenLabel.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            flag = 0;
            self.hidenLabel.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.width);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            self.currentLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            
        }];
    }
}




#pragma mark - 开始／停止定时器

- (void)start {
    //创建定时器
    [self createTimer];
}

- (void)stopTimer {
    //停止定时器
    //在invalidate之前最好先用isValid先判断是否还在线程中：
    if ([_timer isValid] == YES) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)createTimer
{
    _timer=[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(dealTimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}


@end
